﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Microsoft.Win32;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;

namespace Business
{
   public class Configuration
   {
      public Color TextColor;
      public string Nick;
      public string Host;
      public string Port;

      public bool AutoRun;
      public bool AutoConnect;
      public bool RunMinimized;

      public Configuration()
      {
         // Auto load configuration on creation
         Load();
      }

      public void Save()
      {
         // Write preferences
         FileStream fs = new FileStream(GetUserDataPath(), FileMode.Create, FileAccess.Write);
         BinaryFormatter b = new BinaryFormatter();

         b.Serialize(fs, Nick);
         b.Serialize(fs, Host);
         b.Serialize(fs, Port);
         b.Serialize(fs, TextColor);

         b.Serialize(fs, AutoRun);
         b.Serialize(fs, AutoConnect);
         b.Serialize(fs, RunMinimized);

         fs.Close();

         // Special treatement for AutoLoading:
         RegistryKey key = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run", true);
         if (key == null)
         {
            MessageBox.Show("Error accessing registry : can't write autorun parameter", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
         }
         else
         {
            try // May throw an exception if overwrite or delete unexisting value
            {
               if (AutoRun)
               {
                  key.SetValue("BBChat", Application.ExecutablePath);
               }
               else
               {
                  if (key.GetValue("BBChat") != null)
                     key.DeleteValue("BBChat");
               }
            }
            catch (Exception)
            {
            }
         }
      }

      public void Load()
      {
         // Load preferencies
         FileStream fs;
         try
         {
            fs = new FileStream(GetUserDataPath(), FileMode.Open, FileAccess.Read);
            BinaryFormatter b = new BinaryFormatter();
            fs.Seek(0, SeekOrigin.Begin);

            Nick = (string)b.Deserialize(fs);
            Host = (string)b.Deserialize(fs);
            Port = (string)b.Deserialize(fs);
            TextColor = (Color)b.Deserialize(fs);

            AutoRun = (bool)b.Deserialize(fs);
            AutoConnect = (bool)b.Deserialize(fs);
            RunMinimized = (bool)b.Deserialize(fs);

            fs.Close();
         }
         catch (Exception)
         {
            // No preferencies, use default
            Nick = "Your nick";
            Host = "localhost";
            Port = "14242";
            TextColor = Color.Black;

            AutoRun = false;
            AutoConnect = false;
            RunMinimized = false;
         }
      }

      static private string GetUserDataPath()
      {
         string cachedUserDataPath;

         cachedUserDataPath = System.IO.Path.Combine(System.Windows.Forms.Application.UserAppDataPath, "CamViewer");
         string[] pathParts = cachedUserDataPath.Split(System.IO.Path.DirectorySeparatorChar);
         System.Diagnostics.Debug.Assert(pathParts.Length > 3);
         string pathSep = System.Char.ToString(System.IO.Path.DirectorySeparatorChar);
         if (pathParts.Length > 3)
            cachedUserDataPath = System.String.Join(pathSep, pathParts, 0, pathParts.Length - 2);

         cachedUserDataPath += "\\Configuration.bin";
         return cachedUserDataPath;
      }
   }
}
